<?php

namespace seecms\lib\module;

use app\index\controller\Console;
use seecms\db\SeeDbException;
use seecms\See;

/**
 * 发布鸡肋
 */
abstract class ModulePublish
{

    public function vendor()
    {

    }

    public function seed()
    {
        $this->createMenu();
        $this->createRule();
    }

    public function init()
    {
        $this->vendor();
        $this->table();
        $this->seed();
    }

    /**
     * 表文件
     * @return string
     */
    protected function tableFile(): string
    {
        return '';
    }

    /**
     * 创建数据表
     */
    public function table(): bool
    {
        try {
            $table = $this->tableFile();
            if (file_exists($table)) {
                $sql = file_get_contents($table);
                // 分割SQL语句
                $queries = preprocessing_sql($sql);
                // 执行每条SQL
                foreach ($queries as $query) {
                    See::db()->query($query);
                }
            }
        } catch (SeeDbException|\Throwable $exception) {
            \magein\utils\Console::error('  ' . $exception->getMessage());
        }

        return true;
    }

    /**
     * 拼接权限规则
     * @param $group
     * @param $title
     * @param $path
     * @return array
     */
    protected function concatRule($group, $title, $path): array
    {
        return [
            'group' => $group,
            'title' => $title,
            'url' => $path,
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ];
    }

    protected function getRuleList(): array
    {
        return [];
    }

    protected function createRule()
    {
        $lists = $this->getRuleList();

        if ($lists) {
            $table = function () {
                return See::db()->table('see_user_rule');
            };

            foreach ($lists as $item) {
                $data = $table()->where('group', $item['group'])
                    ->where('url', $item['url'])->find();
                if (empty($data)) {
                    $table()->insert($item);
                }
            }
        }
    }

    protected function createMenu()
    {
        $lists = $this->getMenuList();

        if ($lists) {
            foreach ($lists as $item) {
                $data = See::db()->table('see_menu')
                    ->where('parent_id', $item['parent_id'])
                    ->where('title', $item['title'])
                    ->find();
                if (empty($data)) {
                    See::db()->table('see_menu')->insert($item);
                }
            }
        }
    }

    /**
     * 拼接菜单
     * @param $parent_id
     * @param $title
     * @param $icon
     * @param $url
     * @param integer|string $sort
     * @param string $open_type
     * @return array
     */
    protected function concatMenu($parent_id, $title, $icon, $url, $sort = 999, string $open_type = '_iframe'): array
    {
        return [
            'parent_id' => $parent_id,
            'title' => $title,
            'icon' => $icon,
            'url' => $url,
            'sort' => $sort,
            'type' => 1,
            'open_type' => $open_type,
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ];
    }

    protected function getMenuList(): array
    {
        return [];
    }

    protected function getResourceRuleList($group, $title, $controller = ''): array
    {
        $controller = $controller ?: $group;

        return [
            $this->concatRule($group, $title . '列表', $controller . '/index'),
            $this->concatRule($group, $title . '创建', $controller . '/create'),
            $this->concatRule($group, $title . '保存', $controller . '/save'),
            $this->concatRule($group, $title . '查看', $controller . '/read'),
            $this->concatRule($group, $title . '编辑', $controller . '/edit'),
            $this->concatRule($group, $title . '更新', $controller . '/update'),
            $this->concatRule($group, $title . '删除', $controller . '/del'),
        ];
    }

}